package cn.zyy.dao;

import cn.zyy.entity.Course;
import cn.zyy.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 科目持久化层
 */
public class CourseDaoImpl implements CourseDao {

    @Override
    //根据科目名称查询科目ID
    public Course findCourseByCourseName(String courseName) {
        DataSource dataSource = JDBCUtil.getDataSource();
        QueryRunner qrn = new QueryRunner(dataSource);
        String sql = "select course_id as courseId from course where course_name = ?";
        Course course = null;
        try {
            course = qrn.query(sql, new BeanHandler<>(Course.class), courseName);
            return course;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return course;
    }

}
